package com.xi.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xi.springboot.annotation.SysLog;
import com.xi.springboot.entity.*;
import com.xi.springboot.service.UrlService;
import com.xi.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: xjs
 * @Date: 2022/11/19/10:56
 * @Description:
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UrlService urlService;
    @SysLog("控制层:验证登录")
    @GetMapping("/login")
    public String goLogin(){
        return "login";
    }
    @PostMapping ("/login")
    public String login(){
        return "index";
    }
    @SysLog("控制层: 访问登录页面")
    @RequestMapping("/")
    public String loginPage(){
        return "login";
    }
    @SysLog("控制层: 访问首页")
    @GetMapping("/guest")
    public String index(){
        return "index";
    }
//    public String login(HttpServletRequest request){
//        String userName = request.getParameter("username");
//        String password = request.getParameter("password");
//        User userInfo = userService.login(userName, password);
////        userInfo = new User(1L,"xi","rose","U","xi");
//        System.out.println("账号"+userInfo);
//        if (userInfo != null){
//             HttpSession session = request.getSession();
//             session.setAttribute("user",userInfo);
//            System.out.println("session"+session.getAttribute("user"));
//             return "index";
//        }else {
//           return "login";
//        }
//    }

    @GetMapping("/logout")
    public String Logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "/login";
    }
    @GetMapping("/reg")
    public String goRegister(){
     return "register";
    }

    @PostMapping("/reg")
    public String register(HttpServletRequest request,Model model){
            String userName = request.getParameter("username");
            String password = request.getParameter("password");
            Boolean b = userService.insertUser(userName, password);
            String msg = b ? Message.REGISTER_SUCCESS : Message.REGISTER_FAIL;
            if (b){
                model.addAttribute("regRes",new Result(b,msg));
                return "login";
            }else {
                return "register";
            }
    }
    @PostMapping ("/admin")
    public String goAdmin(HttpServletRequest request){
        System.out.println(request.getSession().getAttribute("user"));
        return "admin";
    }
    @GetMapping("/admin")
    @ResponseBody
    public Result getAllUrls(HttpServletRequest request){
        Integer pageId = Integer.valueOf(request.getParameter("pageId"));
        System.out.println("页码"+pageId);
//        IPage<Url> urlIPage = urlService.selectALlUrls(pageId);
        IPage<UrlVo> urlIPage = urlService.selectByPage(pageId);
        long total = urlIPage.getTotal();
        long pages = urlIPage.getPages();
         Record record = new Record(urlIPage,pages,total,pageId);
        return new Result(record);
    }
}
